package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.ReservationService;
import com.zzyl.vo.ReservationCountVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
public class CustomerReservationController extends BaseController {

    @Autowired
    private ReservationService reservationService;

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @GetMapping("/cancelled-count")
    public ResponseResult<Integer> FindBycancelled() {
        Integer num = reservationService.FindBycancelled();
        return success(num);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @return
     */
    @GetMapping("/countByTime")
    public ResponseResult<List<ReservationCountVo>> findCountByTime(@RequestParam(name = "time") Long time) {
        List<ReservationCountVo> voList = reservationService.findCountByTime(time);
        return success(voList);
    }

    /**
     * 新增预约
     *
     * @param dto
     * @return
     */
    @PostMapping
    public ResponseResult save(@RequestBody ReservationDto dto) {
        reservationService.save(dto);
        return success();
    }

    /**
     * 分页查询预约
     *
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<PageResponse<ReservationVo>> findByPage(
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        PageResponse<ReservationVo> page = reservationService.findByPage(status, pageNum, pageSize);
        return success(page);
    }

    /**
     * 取消预约
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public ResponseResult updateStatus(@PathVariable(name = "id") Long id) {
        reservationService.update(id);
        return success();
    }
}
